Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Azure.ClientSdk.Analyzers to fix incorrect behaviors #6496

Closed
wants to merge 15 commits into from

Conversation

pshao25
Copy link
Member

@pshao25 pshao25 commented Jul 12, 2023

When updating Azure.ClientSdk.Analyzers in azure-sdk-for-net to uptake #6156, there are some errors not taken into consideration before:

After this PR, will do Azure/azure-sdk-for-net#37037

Unblocks Azure/azure-sdk-for-net#37447

Overall, together with #6156, the changes are:

  1. AZC0004: From "if there is an async method then it should have corresponding sync method" to "if there is an async method then it should have corresponding sync method, and if there is a sync method it should have corresponding async mthod".
  2. AZC0017: Do ensure convenience method not take RequestContent as parameter type.
  3. AZC0018: Do ensure protocol method take a RequestContext parameter called context and not take models as parameter type or return type.
  4. AZC0019: Check ambiguity for protocol method.

@pallavit
Copy link

@pshao25 Could you please update the PR description with what are the unexpected behaviors?

@jsquire
Copy link
Member

jsquire commented Jul 12, 2023

+1 to Pallavi's request. Given that this change will impact everyone working in the repository, let's please leave detailed context so that we can understand and troubleshoot via search in the future if need be.

Copy link
Member

@jsquire jsquire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking merge until we have more context to understand this PR and the wider team has a chance to offer feedback.

@@ -9,7 +9,7 @@ namespace Azure.ClientSdk.Analyzers.Tests
{
public class AZC0015Tests
{
[Theory]
[Theory(Skip = "We use return type as an exit criteria to check method, so this test set is not applicable until we find a better criteria.")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't skip tests - which of these are valid/invalid? If a different analyzer is catching these errors now, would it make sense to update the test to detect both?

Copy link
Member Author

@pshao25 pshao25 Aug 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these cases, we don't check them anymore. ANY method without valid return type, analyzer just skips check. So these tests are invalid until we find a better way to tell what methods we want to check. This is what you brought up, and @m-nash agrees with you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I'm confused. Isn't AsyncPageable a valid return type? The analyzer should be checking that we have sync and async variants of methods returning pageable.

Would it be possible to separate out the tests return invalid types for service methods from the ones that are testing valid return types?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the result of skipping both these tests is that we don't have any tests for AZ0015, which isn't a good place for us to be for this analyzer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AZ0015 just not effective now.
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't realize that implementing it this way made AZC0015 completely non-functional.

I think AZC0015 is a useful diagnostic error to have running in azure-sdk-for-net. For example, if I write a method that accidentally returns Task<Model> where I meant to return Task<Response<Model>>, I would like a warning to help me find this, and not rely on a human architect catching my mistake.

Is it possible to change the implementation for AZC0018 and AZC0019 so that AZC0015 is still functional? I realize that a lot of services have opted-out of this warning, we should see if we can remove these where it's possible, but that doesn't have to be part of this PR.

@@ -264,33 +289,17 @@ public async Task AZC0018ProducedForMethodsWithRequiredRequestContentAndRequired
using System.Threading.Tasks;
using System.Collections.Generic;

namespace Azure.Core
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we change the logic to detect ambiguity. AssetNotNull is not needed any more to detect ambiguity. Current logic is in https://github.com/Azure/azure-sdk-for-net/pull/37795/files

@@ -16,7 +16,7 @@ public abstract class SymbolAnalyzerBase : DiagnosticAnalyzer

protected INamedTypeSymbol ClientOptionsType { get; private set; }

public override void Initialize(AnalysisContext context)
public sealed override void Initialize(AnalysisContext context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: why make this sealed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was sealed before. I removed it in my previous PR to do some change. Now this change is revert so the sealed need to be added back.

@@ -32,7 +32,6 @@ public class SomeClient
}
}";
await Verifier.CreateAnalyzer(code)
.WithDisabledDiagnostics("AZC0015")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: does this enable AZC0015 for these tests?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just means we don't need to suppress AZC0015. That's it.

AZC0015 is not effective now.

public class SomeClient
{
public virtual CapacityReservationCollection GetCapacityReservations()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we still able to detect and verify that subclient methods need to be virtual?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Being virtual is required for mocking, so we need to be able to enforce it for subclient methods as well)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things here:

  1. I'm not deleting anything. I think you should see all the comments together, not just one iteration.
  2. Subclient cannot be checked any more because its return not is not one of Operation, Pageable, Response.

.RunAsync();
}

[Fact]
public async Task AZC0004NProducedForMethodsWithoutArgMatchedSyncAlternative()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: is the name of this method NotProduced or Produced?

Copy link
Member Author

@pshao25 pshao25 Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be Produced. I'm just adding back the deleted one which called itself NProduced. Updated to Produced.

@@ -21,8 +21,6 @@ public async Task AZC0018NotProducedForCorrectReturnType()

namespace RandomNamespace
{
public class AssetConversion {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious: why is this being removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not removing. I think you are not seeing the right iteration.

Copy link
Member

@annelo-msft annelo-msft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is changing a lot of existing analyzers, where I think we should preserve their functionality. Would it be possible to reduce its scope so that it only adds the protocol-method related analyzers without changing other ones? If we see the need to change existing analyzers to improve what's happening in azure-sdk-for-net, that is great and we should do that, but I think it would be better to file issues for each one we want to make changes to and handle them in separate PRs.

@pshao25
Copy link
Member Author

pshao25 commented Oct 9, 2023

Already implemented this.

@pshao25 pshao25 closed this Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants